package xyf.p04.tree;

import commons.ListNode;
import commons.TreeNode;

import java.util.*;

/**
 * ClassName: M0403_Convert2Node
 * Description:
 * Author: xyf
 * Date: 9/9/22 9:44 AM
 * Version: 1.0
 **/
public class M0403_Convert2Node
{
    public ListNode[] listOfDepth(TreeNode tree)
    {
        if (tree == null) return new ListNode[0];
        List<ListNode> ans = new ArrayList();
        Deque<TreeNode> queue = new ArrayDeque();
        queue.add(tree);
        while (!queue.isEmpty())
        {
            int size = queue.size();
            ListNode cur = null;
            while (size-- > 0)
            {
                TreeNode node = queue.pop();
                ListNode t = new ListNode(node.val);
                t.next = cur;
                cur = t;
                if (node.right != null) queue.add(node.right);
                if (node.left != null) queue.add(node.left);
            }
            ans.add(cur);
        }
        return ans.toArray(new ListNode[ans.size()]);
    }
}